import Vue from 'vue'
import VueRouter from 'vue-router'
import store from "../store"

Vue.use(VueRouter)

//验证某个路由的权限
function checkRouter (path,next){
  if(store.state.info.menus_url.includes(path)){
    next()
  }else{
    next("/")
  }
}

//导出路由
export const IndexRoutes = [
  {
    meta: { title: "菜单管理" },
    path: "menu",
    component: () => import("../pages/menu/menu.vue"),
    //独享守卫
    beforeEnter(to,from,next){
      checkRouter("/menu",next)
    }
  },
  {
    meta: { title: "角色管理" },
    path: "role",
    component: () => import("../pages/role/role.vue"),
    //独享守卫
    beforeEnter(to,from,next){
      checkRouter("/role",next)
    }
  },
  {
    meta: { title: "管理员管理" },
    path: "manage",
    component: () => import("../pages/manage/manage.vue"),
    //独享守卫
    beforeEnter(to,from,next){
      checkRouter("/manage",next)
    }
  },
  {
    meta: { title: "商品分类" },
    path: "cate",
    component: () => import("../pages/cate/cate.vue"),
    //独享守卫
    beforeEnter(to,from,next){
      checkRouter("/cate",next)
    }
  },
  {
    meta: { title: "商品规格" },
    path: "specs",
    component: () => import("../pages/specs/specs.vue"),
    //独享守卫
    beforeEnter(to,from,next){
      checkRouter("/specs",next)
    }
  },
  {
    meta: { title: "商品管理" },
    path: "goods",
    component: () => import("../pages/goods/goods.vue"),
    //独享守卫
    beforeEnter(to,from,next){
      checkRouter("/goods",next)
    }
  },
  {
    meta: { title: "会员管理" },
    path: "vip",
    component: () => import("../pages/vip/vip.vue"),
    //独享守卫
    beforeEnter(to,from,next){
      checkRouter("/vip",next)
    }
  },
  {
    meta: { title: "轮播图管理" },
    path: "banner",
    component: () => import("../pages/banner/banner.vue"),
    //独享守卫
    beforeEnter(to,from,next){
      checkRouter("/banner",next)
    }
  },
  {
    meta: { title: "秒杀活动" },
    path: "seckill",
    component: () => import("../pages/seckill/seckill.vue"),
    //独享守卫
    beforeEnter(to,from,next){
      checkRouter("/seckill",next)
    }
  }
]

const routes = [
  {
    path: "/login",
    component: () => import("../pages/login/login.vue")
  },
  {
    path: "/",
    component: () => import("../pages/index/index.vue"),
    children: [
      {
        path: "",
        component: () => import("../pages/home/home.vue")
      },
      ...IndexRoutes 
    ]
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to,from,next)=>{
  //如果去登录，进
  if(to.path=="/login"){
    next();
    return;
  }
  console.log(store);
  //去的不是登录，查看是否已登录
  if(store.state.info.token){
    next();
    return;
  }
  //没有登陆、
  next("/login")
})
export default router
